﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using OpenSoftStore.Orm.Model;
using OpenSoftStore.BusinessLogic.Set;

namespace OpenSoftStore.Client.UIL.ManageSet
{
    public partial class ShopAdd : UserControl
    {
        private string shopId;
        private SetLogic setLogic;
        public ShopAdd()
        {
            InitializeComponent();
            setLogic = new SetLogic();
        }
        public void GetID(string Id)
        {
            shopId = Id;
            GetShopInfo();
        }

        private void GetShopInfo()
        {
            T_Shop shopModel = setLogic.SqlWhere<T_Shop>(" Id='" + shopId + "'").ToList()[0];
            tbLxr.Text = shopModel.Manager;
            tbDm.Text = shopModel.Name;
            tbQq.Text = shopModel.Qq;
            tbRemark.Text = shopModel.Remark;
            tbTal.Text = shopModel.Tel;
            tbAddress.Text = shopModel.Address;
            tbFax.Text = shopModel.Fax;
        }

        private void BtnSave_Click(object sender, EventArgs e)
        {
            T_Shop shop = new T_Shop();
            shop.Id = shopId;
            shop.Address = tbAddress.Text;
            shop.AddTime = DateTime.Now;
            shop.Fax = tbFax.Text;
            shop.Name = tbDm.Text;
            shop.Qq = tbQq.Text;
            shop.Tel = tbTal.Text;
            shop.Remark = tbRemark.Text;
            shop.Manager = tbLxr.Text;

            if (!string.IsNullOrEmpty(shop.Id))
                setLogic.Update(shop);
            else
                setLogic.Add(shop);
        }

        private void glassButton1_Click(object sender, EventArgs e)
        {
            T_Shop shop = new T_Shop();
            shop.Id = shopId;
            shop.Address = tbAddress.Text;
            shop.AddTime = DateTime.Now;
            shop.Fax = tbFax.Text;
            shop.Name = tbDm.Text;
            shop.Qq = tbQq.Text;
            shop.Tel = tbTal.Text;
            shop.Remark = tbRemark.Text;
            shop.Manager = tbLxr.Text;

            int row = 0;
            row = !string.IsNullOrEmpty(shop.Id) ? setLogic.Update(shop) : setLogic.Add(shop);
            var findForm = FindForm();
            if (findForm == null) return;
            if (row == 0) findForm.DialogResult = DialogResult.Cancel;
            else ShopList.Instance.GetDgvSource();
            findForm.Close();
        }

        private void BtnCancel_Click(object sender, EventArgs e)
        {
            var findForm = FindForm();
            if (findForm == null) return;
            findForm.DialogResult = DialogResult.Cancel;
            findForm.Close();
        }

        private void pbClose_Click(object sender, EventArgs e)
        {
            var findForm = FindForm();
            if (findForm == null) return;
            findForm.DialogResult = DialogResult.Cancel;
            findForm.Close();
        }
    }
}
